package com.zhb.zhbaiagent.rag;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 实现初始化向量数据库并且保存文档的方法
 */
@Configuration
@Slf4j
public class LoveAppVectorStoreConfig {
    //注入文档加载器，用于加载配置文件的数据
    @Resource
    private LoveAppDocumentLoader loveAppDocumentLoader;
    @Resource
    private MyTokenTextSplitter myTokenTextSplitter;
    @Resource
    private MyKeyEnricher myKeyEnricher;
    @Bean
    VectorStore  loveAppVectorStore(EmbeddingModel dashscopeEmbeddingModel) {
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(dashscopeEmbeddingModel).build();
        //加载文档
        List<Document> documentList = loveAppDocumentLoader.loadMarkdowns();
        //使用自定义切割器
//        List<Document>splitCustomized  = myTokenTextSplitter.splitCustomized(documentList);
        //使用AI解析关键词并填充到元信息中
        List<Document> enrichDocumets = myKeyEnricher.enrichDocumets(documentList);
        simpleVectorStore.add(enrichDocumets);
        return simpleVectorStore;
    }


}
